<?php

namespace App\Http\Controllers\home;

use App\Http\Model\Categroy;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\home\BaseController;
use Cookie;
use App\Http\Model\Goods;
/**
 * Class IndexController 前台首页控制器
 * @package App\Http\Controllers\home
 */
class IndexController extends BaseController
{
   /**
    * 加载前台首页
    */
    public function index(){
        /** 获取所有首页栏目导航 */
        $cat_list = (new Categroy())->getIndexCate();

        /** 获取最新商品商品 */
        $new_list = Goods::where('is_new','1')->take(5)->get();
        /** 获取精品商品 */
        $best_list = Goods::where('is_best','1')->take(5)->get();
        /** 获取热销商品 */
        $hot_list = Goods::where('is_hot','1')->take(5)->get();

        /** 展示顶级1商品 */
        $goods = $this -> getIndexGoods(1);

        /** 整合数据加载首页视图 */
        return view('home.index.index',[
                'cat_list'=>$cat_list,
                'new_list'=>$new_list,
                'best_list'=>$best_list,
                'hot_list'=>$hot_list,
                'goods' => $goods
            ]
        );
    }

    /***
     * 获取顶级栏目商品
     * @param int $count 取出数量
     */
    public function getIndexGoods($count){
        $all_cat = Categroy::all();
        $top_cat = Categroy::where('parent_id',0)->take($count)->get();
        /** 获取该栏目下所有子栏目id */
        $son_list = (new Categroy()) -> getTree($all_cat,$top_cat[0]->cat_id);
        $id_list = array();
        foreach($son_list as $cat){
            $id_list[] = $cat->cat_id;
        }

        /** 集中数据 */
        $goods = Goods::whereIn('cat_id',$id_list)->get();
        return $goods;
    }
}
